package edu.cmu.cs214.hw9.db.DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import edu.cmu.cs214.hw9.db.SQLiteAdapter;
import edu.cmu.cs214.hw9.resources.Constants;

/**
 * Handles looking up and inserting into the Status table.
 * @author Peter
 *
 */
public class FriendDAO extends SQLiteAdapter {

	public FriendDAO() throws Exception 
	{
		super();
	}
	
	/**
	 * Finds a friend given the friend's id.
	 * @param id
	 * @return
	 */
	public Friend findFriend(int user1Id, int user2Id)
	{
		Friend ret = null;
		ResultSet rs = null;
		try 
		{
			String statement = "SELECT * FROM " + Constants.FRIENDS_TABLE + " WHERE user1_id=? AND user2_id=?;";
			PreparedStatement ps = conn.prepareStatement(statement);
			ps.setString(1, user1Id + "");
			ps.setString(2, user2Id + "");
			
			rs = ps.executeQuery();
			while(rs.next())
				ret = new Friend(rs.getInt("id"), rs.getInt("user1_id"), rs.getInt("user2_id"), rs.getInt("type"), rs.getInt("pending"));
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		} 
		finally 
		{
            try
            {
            	if(rs != null)
            		rs.close();
            } 
            catch (SQLException e)
            {
            }
		}
		return ret;
	}
	
	/**
	 * Creates a status or notification depending on type.
	 * @param userId
	 * @param msg
	 * @param type
	 * @return true if successful, false otherwise
	 */
	private boolean createOrUpdate(int user1Id, int user2Id, int type, int pending)
	{
		Friend friend = findFriend(user1Id, user2Id);
		if(friend == null)
		{
			PreparedStatement ps;
			String statement = "INSERT INTO " + Constants.FRIENDS_TABLE + " (user1Id, user2Id, type, pending) VALUES (?, ?, ?, ?)";
			try
			{
				ps = conn.prepareStatement(statement);
				ps.setInt(1, user1Id);
				ps.setInt(2, user2Id);
				ps.setInt(3, type);
				ps.setInt(4, pending);
				ps.executeUpdate();
			} 
			catch(SQLException e)
			{
				e.printStackTrace();
				return false;
			}
			
			return true;
		}
		else
		{
			PreparedStatement ps;
			String statement = "UPDATE " + Constants.FRIENDS_TABLE + " SET type=?, pending=? WHERE id=?";
			try{
				ps = conn.prepareStatement(statement);
				ps.setInt(1, user1Id);
				ps.setInt(2, user2Id);
				ps.setInt(3, friend.getId());
				ps.executeUpdate();
			} 
			catch(SQLException e)
			{
				return false;
			}
			
			return true;
		}
	}

	public boolean createFriend(int user1Id, int user2Id)
	{		
		return createOrUpdate(user1Id, user2Id, 1, 0) &&
				createOrUpdate(user2Id, user1Id, 1, 0);
		
	}
	
	public boolean createSubscription(int user1Id, int user2Id)
	{		
		return createOrUpdate(user1Id, user2Id, 2, 0);
	}
	
	public boolean createFriendRequest(int user1Id, int user2Id)
	{
		Friend friend = findFriend(user1Id, user2Id);
		if(friend == null)
			return createOrUpdate(user1Id, user2Id, 3, 1);	
		else
			return createOrUpdate(user1Id, user2Id, friend.getType(), 1);
		
	}
}